 /*      SClassString.cpp
 *
 *      Copyright 2009-2010 virpool <virpool@ukrpost.ua>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include "SClassString.h"
#include "SClassInteger.h"
#include "SClassArray.h"

SClassString::SClassString(std::string name) : SObject(name, "String")
{
	// TODO Auto-generated constructor stub
	GetMethods()->erase("ToString");
	GetMethods()->insert(pair<std::string, s_func>("ToString", &SClassString::ToString));
	GetMethods()->insert(pair<std::string, s_func>("ConvToInt", &SClassString::ConvToInt));
	GetMethods()->insert(pair<std::string, s_func>("Split", &SClassString::Split));
	GetMethods()->insert(pair<std::string, s_func>("Length", &SClassString::Length));
	GetMethods()->insert(pair<std::string, s_func>("Invert", &SClassString::Invert));
	GetMethods()->insert(pair<std::string, s_func>("IndexOf", &SClassString::IndexOf));
	GetMethods()->insert(pair<std::string, s_func>("LastIndexOf", &SClassString::LastIndexOf));

}

SClassString::~SClassString()
{
	// TODO Auto-generated destructor stub
}

int SClassString::ConvToInt(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || args == NULL)
		return S_INVOKE_INCORRECT;
	param_list::iterator p = args->begin();
	if (p->type != S_OBJECT)
	{
		return S_INVOKE_INCORRECT;
	}
	if (((SClassInteger*)p->val)->GetClassName() != "Integer")
		return S_INVOKE_INCORRECT;
	((SClassInteger*)p->val)->Set(atoi(((SClassString*)obj)->Get().c_str()));
	return S_INVOKE_OK;
}

int SClassString::Split(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || args == NULL)
		return S_INVOKE_INCORRECT;
	param_list::iterator p = args->begin();
	if (p->type != S_STRING)
		return S_INVOKE_INCORRECT;
	std::string delim = *((std::string*) p->val);
	p++;
	if (p->type != S_OBJECT)
		return S_INVOKE_INCORRECT;
	SObject *ar = (SObject*) p->val;
	if (ar->GetClassName() != "Array")
		return S_INVOKE_INCORRECT;
	if (((SClassArray*)ar)->GetArrayType() != "String")
		return S_INVOKE_INCORRECT;
	
	std::string s = ((SClassString*)obj)->Get();
	size_t pn = 0, prev = 0;
	param_list ps;
	s_param for_res;
	while ( ( pn = s.find(delim, pn) ) != -1 )
	{
		std::string *sd = new std::string;
		*sd = s.substr(prev, pn - prev);
		prev = pn + 1;
		pn++;
		s_param t;
		t.type = S_STRING;
		t.val = sd;
		ps.push_back(t);
		ar->Invoke(std::string("Add"), &for_res, &ps);
		delete sd;
		ps.clear();
	}
	std::string *sd = new std::string;
	*sd = s.substr(prev, s.size() - 1);
	s_param t;
	t.type = S_STRING;
	t.val = sd;
	ps.push_back(t);
	ar->Invoke(std::string("Add"), &for_res, &ps);
	delete sd;
	return S_INVOKE_OK;
}

int SClassString::ToString(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || res == NULL)
	{
		return S_INVOKE_INCORRECT;
	}
	res->type = S_STRING;
	std::string *s = new std::string();
	(*s) = ((SClassString*)obj)->Get();
	res->val = s;
	return S_INVOKE_OK;
}

int SClassString::Length(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || res == NULL)
	{
		return S_INVOKE_INCORRECT;
	}
	res->type = S_INT;
	int *l = new int;
	*l = ((SClassString*) obj)->Get().size();
	res->val = l;
	return S_INVOKE_OK;
}

int SClassString::Invert(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL)
	{
		return S_INVOKE_INCORRECT;
	}
	std::string s = ((SClassString*) obj)->Get(), ress;
	for ( int i = s.size() - 1; i >= 0; i-- )
		ress += s[i];
	((SClassString*) obj)->Set(ress);
	return S_INVOKE_OK;
}

int SClassString::IndexOf(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || res == NULL || args == NULL || ! args->size() )
	{
		return S_INVOKE_INCORRECT;
	}

	param_list::iterator p = args->begin();

	if ( (*p).type != S_STRING )
		return S_INVOKE_INCORRECT;

	res->type = S_INT;
	int *l = new int;

	*l = ((SClassString*) obj)->Get().find( *((std::string*) (*p).val), 0 );
	res->val = l;
	return S_INVOKE_OK;
}

int SClassString::LastIndexOf(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || res == NULL || args == NULL || ! args->size() )
	{
		return S_INVOKE_INCORRECT;
	}

	param_list::iterator p = args->begin();

	if ( (*p).type != S_STRING )
		return S_INVOKE_INCORRECT;

	res->type = S_INT;
	int *l = new int;

	*l = ((SClassString*) obj)->Get().find_last_of( *((std::string*) (*p).val), 0 );
	res->val = l;
	return S_INVOKE_OK;
}
